package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CartController {
    /**
     * 商品信息页面,提交新增购物车数据,存储到redis
     */
    @Autowired
    private IOmsCartService cartService;
    @PostMapping("/oms/cart/add")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult addCart(CartAddDTO cartAddDTO){
        //缺少userId,从jwt解析出来
        cartService.addCart(cartAddDTO);
        return JsonResult.ok();
    }
    /**
     * 根据接口,完成查询我的购物车方法
     */
    @GetMapping("/oms/cart/list")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<JsonPage<CartStandardVO>> listMyCarts(){
        JsonPage<CartStandardVO> jsonPage = cartService.listCarts(null, null);
        return JsonResult.ok(jsonPage);
    }
    /**
     *购物车更新,提交的quantity就是我们要覆盖到redis
     */
    @PostMapping("/oms/cart/update/quantity")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult updateCartQuantity(CartUpdateDTO cartUpdateDTO){
        cartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok();
    }
}
